import 'dart:math';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:meiyunapp/common/url_base.dart';
import 'package:meiyunapp/entity/home_top_data_entity.dart';
import 'package:meiyunapp/page/type/custom_painter_page.dart';

///各个入口
class HomeTypeComponent extends StatefulWidget {
  final List<HomeTopDataCategories>? categories;

  const HomeTypeComponent(this.categories);

  @override
  State<HomeTypeComponent> createState() => _HomeTypeComponentState();
}

class _HomeTypeComponentState extends State<HomeTypeComponent> {
  final List<Widget> gridviews = [];
  int currentPageIndex = 0;

  @override
  void initState() {
    gridviews.clear();
    initPage();
    super.initState();
  }

  void initPage() {
    int count = 0;
    for (int i = 0; i < widget.categories!.length; i++) {
      if (i % 8 == 0) {
        int end = min(count * 8 + 8, widget.categories!.length);
        var temp = widget.categories!.sublist(count * 8, end);
        gridviews.add(buildGridView2(temp));
        count++;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.categories == null || widget.categories!.isEmpty) {
      return const SizedBox();
    }
    return buildPageView();
  }

  Widget buildPageView() {
    return SizedBox(
      height: 200,
      child: Stack(
        children: [
          PageView(
            children: gridviews,
            onPageChanged: (i) {
              setState(() {
                currentPageIndex = i;
              });
            },
          ),
          Positioned(
            bottom: 10,
            left: 0,
            right: 0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(gridviews.length, (index) {
                return Container(
                  margin: const EdgeInsets.symmetric(horizontal: 5),
                  width: 8,
                  height: 8,
                  decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      color: currentPageIndex == index
                          ? Colors.blue
                          : Colors.grey),
                );
              }).toList(),
            ),
          ),
        ],
      ),
    );
  }

  Widget buildGridView2(List<HomeTopDataCategories> temp) {
    return GridView.builder(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      padding: EdgeInsets.zero,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 4,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
        childAspectRatio: 1.0,
      ),
      itemBuilder: (BuildContext context, int index) {
        return buildItem(temp[index]);
      },
      itemCount: temp.length,
    );
  }

  Widget buildItem(HomeTopDataCategories covariant) {
    return InkWell(
      onTap: () {
        //Get.to(const GoodsTypePage());
        Get.to(const CustomPainterPage());
      },
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          CachedNetworkImage(
            imageUrl: UrlBase.getImage(covariant.icon),
            width: 45,
            height: 45,
            placeholder: (context, url) => const CupertinoActivityIndicator(),
            errorWidget: (context, url, error) => const Icon(Icons.error),
          ),
          const SizedBox(height: 6),
          Text(covariant.name)
        ],
      ),
    );
  }
}
